<?php

namespace app\index\controller;

class ResearchCopy extends Base
{
    //前置方法
    protected $beforeActionList = [
        'rquestion' => ['only' => 'detailed'],
        'right' => ['only' => 'index,detailed'],
    ];


    public function rquestion()
    {
        $id = $this->request->param('id');

        $list = cache('question_copy' . $id . cookie('think_var'));

        if (empty($list)) {
            $rqdata = db('Research_question_copy')->where(array('rid' => $id))->order('id asc')->select();
            $useqids = $list = array();
            $time = substr(time(), -1);
            foreach ($rqdata as $v) {
                $useqids[] = $v['id'];
                switch ($time) {
                    case '1':
                        $oredr = 'name asc';
                        break;
                    case '3':
                    case '5':
                        $oredr = 'id desc';
                        break;
                    case '7':
                    case '9':
                        $oredr = 'id asc';
                        break;
                    case '2':
                    case '4':
                        $oredr = 'question asc';
                        break;
                    case '6':
                    case '8':
                        $oredr = 'question desc';
                        break;
                    default:
                        $oredr = 'name desc';
                        break;
                }
                $answerobj = db('Research_answer_copy')->where(array('qid' => $v['id']))->order($oredr)->select();
                $v['answer'] = $answerobj;
                $v['rowspan'] = count($v['answer']);
                $list[] = $v;
            }
            cache('question_copy' . $id . cookie('think_var'), $list, 600);

        }

        $this->assign([
                'list' => $list,
            ]
        );

    }


    /**
     * 保存调研结果
     */
    public function saveresearch()
    {

        //登陆考试
        $Admin = session('User', '', 'User');
        if (empty($Admin->id)) {
            return ajax_info('请登录考试', 'y', url('user/login/index'));
        }
        $oo = $this->request->param();

//        exit();
//        $dateline = db('research_copy')->where('id', $oo['id'])->value('dateline');
//        if($dateline<time()){
//            return ajax_info('已经超过时间','u');
//        }
//
//        $o = db('research_result')->where('rid', $oo['id'])->find();
//        if ($o) {
//            return ajax_info('已经提交过', 'u');
//        }
//
//
        if (empty($oo['oid'])) {
            return ajax_info('最少做一个选择');
        }
        $count = null;
        foreach ($oo['oid'] as $k => $v) {
            if (is_array($v)) {
//                print_r($v);
                $vvdate = array();
                foreach ($v as $vv) {
                    $vvdate[] = $vv;
                }
                asort($vvdate);
                $v = implode('', $vvdate);
                $value[$k] = $v;
            } elseif (!empty($v)) {
                $value[$k] = $v;
            }

            $question = db('research_question_copy')->where('id', $k)->where('answer', $v)->where('type', 'neq', 2)->value('point');
            $count += $question;

        }


//        $date = implode(',', $value);

//        print_r($data);
//        print_r($oo);
//        print_r($date);
//        print_r(json_encode($value));
//
//        print_r($value);
//
//        exit();

        $typs = db('research_question_copy')->where('rid', $oo['id'])->where('type', 2)->find();

        $data = array(
            'rid' => $oo['id'],
            'qid' => $Admin->id,
            'url' => str_replace($this->request->domain(), '', $_SERVER['HTTP_REFERER']),
            'username' => $Admin->name,
            'title' => $oo['title'],
            'score' => $typs ? null : $count,
            'content' => json_encode($value),
            'lang' => cookie('think_var'),
            'create_time' => time(),
        );
        db('research_result_copy')->insert($data);

        return ajax_info('成功交卷', 'u');


    }


}
